package topcbl.taxi;

import java.util.ArrayList;

import topcbl.data.Car;
import topcbl.net.TaxiService;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class CallTaxiMapFragment extends Fragment {
	Marker mUserMarker;
	GoogleMap map;
	ArrayList<Car> carlist;
	ArrayList<Marker> carmarkerlist = new ArrayList<Marker>();
	TaxiService service = MainActivity.mBindService.getService();
	LatLng mFirstPlace, mSecondPlace;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_testmap, container,
				false);
		createMap();
		Intent receiveIntent = getActivity().getIntent();
		double lat = receiveIntent.getDoubleExtra("lat", 0);
		double lng = receiveIntent.getDoubleExtra("lng", 0);
		// --- find user location firstly
		map.clear();
		mFirstPlace = new LatLng(lat, lng);
		map.animateCamera(CameraUpdateFactory.newLatLngZoom(mFirstPlace, 16),
				10, null);
		mUserMarker = putMarkerOnMap(mFirstPlace.latitude,
				mFirstPlace.longitude, R.drawable.user_pin);
		mUserMarker.showInfoWindow();
		service.setFirstPlace(mFirstPlace);
		/*
		 * putMakerOnMap(gpsTracker.getLatitude()+0.001F,
		 * gpsTracker.getLongitude()+0.001F, R.drawable.taxi_pin_1);
		 */

		map.setOnMarkerClickListener(new OnMarkerClickListener() {

			@Override
			public boolean onMarkerClick(Marker arg0) {
				if (arg0.equals(mUserMarker))
					return false;
				for (int i = 0; i < carmarkerlist.size(); i++)
					if (carmarkerlist.get(i).equals(arg0)) {
						Car curCar = carlist.get(i);
						// show dialog when getCarInfo done
						service.getCarInfo(getActivity(), curCar.getStrTEID());
						mSecondPlace = new LatLng(curCar.getLat(), curCar
								.getLng());
						service.setSecondPlace(mSecondPlace);
						service.setCurTEID(curCar.getStrTEID());
					}
				return true;
			}
		});
		return rootView;
	}

	public void addAllTaxiOnMap(ArrayList<Car> carlist) {
		this.carlist = carlist;
		map.clear();
		carmarkerlist.removeAll(carmarkerlist);
		putMarkerOnMap(mFirstPlace.latitude, mFirstPlace.longitude, R.drawable.user_pin);
		for (int i = 0; i < carlist.size(); i++) {
			Car car = carlist.get(i);
			carmarkerlist.add(putMarkerOnMap(car.getLat(), car.getLng(),
					R.drawable.taxi_pin_1));
		}
	}

	private void createMap() {
		map = ((SupportMapFragment) getFragmentManager().findFragmentById(
				R.id.map)).getMap();
		// map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
		// map.setMapType(GoogleMap.MAP_TYPE_NONE);
		map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
		// map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
		// map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

		// --- set Event to put marker when user long click
	}

	public Marker putMarkerOnMap(double lat, double lon, int logo) {
		if (map != null) {
			MarkerOptions mp = new MarkerOptions();
			mp.position(new LatLng(lat, lon));
			mp.title(getString(R.string.markertext));
			mp.icon(BitmapDescriptorFactory.fromResource(logo));
			return map.addMarker(mp);
		}
		return null;
	}

	@Override
	public void onDestroyView() {
		SupportMapFragment f = (SupportMapFragment) getFragmentManager()
				.findFragmentById(R.id.map);

		if (f != null) {
			try {
				getFragmentManager().beginTransaction().remove(f).commit();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		super.onDestroyView();
	}
}
